package com.ja.openchat.ui.message.adapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.config.AppConfigInfo;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.user.OrgUserInfo;
import com.zy.openchat.core.model.user.UserInfo;
import com.ja.openchat.views.UserAvatarView;
import com.zy.openchat.util.Common;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchContactAdapter extends BaseQuickAdapter<ChatInfo, BaseViewHolder> {

    public SearchContactAdapter(List<ChatInfo> fullInfos) {
        super(R.layout.adapter_search_contact_list,fullInfos);
        addChildClickViewIds(R.id.add_btn);
    }
    private OrgUserInfo loginOrgUserInfo;
    public void setLoginOrgUserInfo(OrgUserInfo orgUserInfo){
        loginOrgUserInfo = orgUserInfo;
        notifyDataSetChanged();
    }

    private HashMap<Long, OrgUserInfo> orgUserInfos = new HashMap<>();

    private OrgUserInfo getOrgUserInfoByUserId(final long userId){
        if(orgUserInfos.containsKey(userId)){
            return orgUserInfos.get(userId);
        }
        TelegramManager.getTelegramManager().requestOrgContactInfo(getContext(), userId, new TgResultBlock<OrgUserInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, OrgUserInfo obj) {
                updateOrgUserInfo(userId,obj);
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
            }
        });
        return null;
    }

    private void updateOrgUserInfo(long userId,OrgUserInfo orgUserInfo){
        if(orgUserInfo==null){
            return;
        }
        orgUserInfos.put(userId,orgUserInfo);
        if(!Common.isEmpty(getData())){
            int length = getData().size();
            for(int i = 0;i<length;i++){
                ChatInfo userFullInfo = getItem(i);
                if(!userFullInfo.isGroup()&&userFullInfo.type!=null&&userFullInfo.type.user_id==userId){
                    notifyItemChanged(i);
                    break;
                }
            }
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, ChatInfo userFullInfo) {
        if(userFullInfo==null){
            return;
        }
        UserAvatarView iv_icon = helper.getView(R.id.iv_icon);
        helper.setText(R.id.iv_title,userFullInfo.title);
        if(!userFullInfo.isGroup()){
            helper.setText(R.id.add_btn,getContext().getString(R.string.add_friend_text));
            UserInfo userInfo = UserInfo.getUserInfo(userFullInfo.type.user_id);
            if(userInfo!=null){
                if(userInfo.profile_photo!=null&&!userInfo.profile_photo.isSmallPhotoDownloaded()) {
                    userFullInfo.isAvaterDownloaded = false;
                }else{
                    userFullInfo.isAvaterDownloaded = true;
                }
                iv_icon.showUser(userInfo);
                helper.setGone(R.id.add_btn,userInfo.is_contact);
            }else{
                userFullInfo.isAvaterDownloaded = false;
                iv_icon.showName(userFullInfo.title);
                TelegramManager.getTelegramManager().getUserSimpleInfo(userFullInfo.type.user_id);
                helper.setGone(R.id.add_btn,false);
            }
            AppConfigInfo info = AppConfigInfo.getAppConfigInfo();
            if(info!=null&&info.onlyWhiteAddFriend){
                if(loginOrgUserInfo==null||!loginOrgUserInfo.isInternal){
                    OrgUserInfo orgUserInfo = getOrgUserInfoByUserId(userFullInfo.type.user_id);
                    if(orgUserInfo==null||!orgUserInfo.isInternal){
                        helper.setGone(R.id.add_btn,true);
                    }
                }
            }
        }else if(userFullInfo.isSuperGroup()){
            helper.setText(R.id.add_btn,getContext().getString(R.string.join_text));
            helper.setGone(R.id.add_btn,userFullInfo.isMine);
            iv_icon.showGroup(userFullInfo);
        }

    }
}
